package com.dmgis.hndz.sddz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmgis.entity.DmMapPoint;
import com.dmgis.hndz.common.utils.StringUtil;
import com.dmgis.hndz.mapper.Zhaa11aMapper;
import com.dmgis.hndz.pojoVo.DisasterPositionVo;
import com.dmgis.hndz.pojoVo.DisasterTypeNumVo;
import com.dmgis.hndz.pojoVo.DisasterVo;
import com.dmgis.hndz.sddz.pojo.Zdda01a;
import com.dmgis.hndz.sddz.service.Zdda01aService;
import com.dmgis.hndz.sddz.mapper.Zdda01aMapper;
import com.dmgis.weblib.MapCommon;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 肖伟湘
 * @description 针对表【zdda01a(行政区划表)】的数据库操作Service实现
 * @createDate 2024-07-03 13:33:12
 */
@Service
public class Zdda01aServiceImpl extends ServiceImpl<Zdda01aMapper, Zdda01a>
        implements Zdda01aService {
    @Resource
    private Zdda01aMapper Zdda01aMapper;
    @Resource
    private Zhaa11aMapper zhaa11aMapper;

    @Override
    public Map<String, Object> queryXzqh(String type) {
        Zdda01aMapper.queryXzqh(type);
        return null;
    }

    @Override
    public String queryCityName(String cityCode) {
        return Zdda01aMapper.queryCityName(cityCode);
    }

    @Override
    public List<DisasterTypeNumVo> dzTable(String point, String code) {
        if (StringUtil.IsNullOrEmpty(point)) {
            return Collections.emptyList();
        }
        List<DisasterTypeNumVo> listMap = new ArrayList<>();
        String str = point.replace("*1-", ";");
        String strList = str.replace("*", ";");
        String points = strList.replace(" ", ";");
        String[] pointsList = points.split(";");
        List<DmMapPoint> dmMapPoints = new ArrayList<>();
        for (int i = 0; i < pointsList.length; i = i + 2) {
            DmMapPoint dmMapPoint = new DmMapPoint();
            dmMapPoint.setX(Double.valueOf(pointsList[i]));
            dmMapPoint.setY(Double.valueOf(pointsList[i + 1]));
            dmMapPoints.add(dmMapPoint);
        }
        // 灾害点数据
        List<DisasterVo> disasterList = new ArrayList<>();
        String codeType = "";
        if (code.endsWith("0000")) {
            // 省级
            codeType = code.substring(0, 2);
            disasterList = zhaa11aMapper.getDisasterProvince(codeType);
        } else if (code.endsWith("00")) {
            // 市级
            codeType = code.substring(0, 4);
            disasterList = zhaa11aMapper.getDisasterCity(codeType);
        } else {
            // 县级
            codeType = code.substring(0, 6);
            disasterList = zhaa11aMapper.getDisasterCounty(codeType);
        }

        List<DisasterVo> list = new ArrayList<>();

        list.addAll(disasterList);

        // 去掉不在区域内的数据
        List<DisasterVo> voList = list.stream().filter(
                i -> {
                    return MapCommon.pointWithinPolygon(dmMapPoints, i.getLon(), i.getLat());
                }
        ).collect(Collectors.toList());
        // 根据行政编码去重
        List<DisasterVo> codeList = new ArrayList<>();
        if (code.endsWith("0000")) {
            // 省级
            codeList = voList.stream()
                    .collect(Collectors.collectingAndThen(
                            Collectors.toMap(DisasterVo::getCountyCode, obj -> obj, (e1, e2) -> e1),
                            map -> new ArrayList<>(map.values())
                    ));
        } else {
            // 市级和县级
            codeList = voList.stream()
                    .collect(Collectors.collectingAndThen(
                            Collectors.toMap(DisasterVo::getTownCode, obj -> obj, (e1, e2) -> e1),
                            map -> new ArrayList<>(map.values())
                    ));
        }


        // 统计各个地区灾害点数
        for (DisasterVo disasterVo : codeList) {
            DisasterTypeNumVo disasterTypeNumVo = new DisasterTypeNumVo();
            // 去重code
            String repeatCode;
            if (code.endsWith("0000")) {
                repeatCode = disasterVo.getCountyCode();
            } else {
                // 市级和县级
                repeatCode = disasterVo.getTownCode();
            }
            // 滑坡
            Integer hp = 0;
            // 崩塌
            Integer bt = 0;
            // 泥石流
            Integer nsl = 0;
            // 地区名
            for (DisasterVo vo : voList) {
                // 灾害类型(01:滑坡 02:崩塌 03:泥石流 04:地面塌陷 06:地裂缝 07:地面沉降 00:不稳定斜坡
                // 全部code
                String allCode;
                if (code.endsWith("0000")) {
                    allCode = vo.getCountyCode();
                } else {
                    // 市级和县级
                    allCode = vo.getTownCode();
                }
                String dzName = vo.getDzName();
                if (allCode.equals(repeatCode)) {
                    if ("01".equals(dzName)) {
                        hp++;
                    } else if ("02".equals(dzName)) {
                        bt++;
                    } else if ("03".equals(dzName)) {
                        nsl++;
                    }
                }
            }
            int sum = hp + bt + nsl;
            // 过滤无灾害点地区
            if (sum > 0) {
                disasterTypeNumVo.setHp(hp);
                disasterTypeNumVo.setBt(bt);
                disasterTypeNumVo.setNsl(nsl);
                disasterTypeNumVo.setSum(sum);

                if (disasterVo.getCityCode() != null) {
                    disasterTypeNumVo.setCityCode(Long.valueOf(disasterVo.getCityCode()));
                }
                disasterTypeNumVo.setCityName(disasterVo.getCityName());
                disasterTypeNumVo.setCountyName(disasterVo.getCountyName());
                if (disasterVo.getCountyCode() != null) {
                    disasterTypeNumVo.setCountyCode(Long.valueOf(disasterVo.getCountyCode()));
                }
                disasterTypeNumVo.setTownCode(disasterVo.getTownCode());
                disasterTypeNumVo.setTownName(disasterVo.getTownName());
                listMap.add(disasterTypeNumVo);
            }
        }
        if (listMap.size() > 0) {
            // 根据行政编码排序
            if (code.endsWith("0000")) {
                Collections.sort(listMap, Comparator.comparingLong(DisasterTypeNumVo::getCityCode));
            } else if (code.endsWith("00")) {
                Collections.sort(listMap, Comparator.comparingLong(DisasterTypeNumVo::getCountyCode));
            }
        }
        return listMap;
    }

    /**
     * 查询灾害点经纬度
     *
     * @param code 行政编码
     * @param type 灾害类型(01:滑坡 02:崩塌 03:泥石流 04:地面塌陷 06:地裂缝 07:地面沉降 00:不稳定斜坡
     * @return
     */
    public List<DisasterPositionVo> getDisasterList(String code, String type) {
        String codeType = "";
        if (code.endsWith("0000")) {
            // 省级
            codeType = code.substring(0, 2);
        } else if (code.endsWith("00")) {
            // 市级
            codeType = code.substring(0, 4);
        } else {
            // 县级
            codeType = code.substring(0, 6);
        }
        return zhaa11aMapper.getDisasterList(codeType, type);
    }

}




